<?php

/**
 * 单页文档 - 数据模型
 * 
 * @package    01CMS
 * @subpackage admin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class SingleModel
{
    public $id;
    public $data;
    
    private $Db;

    function __construct ($id = 0)
    {
        $this->Db = model('Db');
        
        if ($id > 0)
        {
            $this->id = $id;
            $this->data = $this->getArchiveById($id);
        }
    }

    /**
     * 通过文档ID获取文档
     * 
     * @param int $id
     * @return object
     * @access public
     */
    function getArchiveById ($id)
    {
        return $this->Db->getRow("SELECT * FROM @@__archive_single where id={$id}");
    }

    /**
     * 获取文档ID，主要用于HTML更新
     * 
     * @param string $where
     * @param int $page
     * @param int $pageSize
     * @return object
     * @access public
     */
    function getArchiveIds ($where = 1, $page = 1, $pageSize = 20)
    {
        $start = ($page - 1) * $pageSize;
        
        return $this->Db->getRows("SELECT id FROM @@__archive_single WHERE {$where} LIMIT {$start}, {$pageSize}");
    }

    /**
     * 计算文档总数，用于HTML更新
     * 
     * @param string $where
     * @return int
     * @access public
     */
    function countArchives ($where = 1)
    {
        return $this->Db->getOne("SELECT count(*) FROM @@__archive_single WHERE {$where}");
    }

    public function __get ($key)
    {
        if (isset($this->$key))
        {
            return $this->$key;
        }
        else if (isset($this->data->$key))
        {
            return $this->data->$key;
        }
        else
        {
            return '';
        }
    }
}